package com.jackstraw.jack_base.rules.service.impl;

import com.jackstraw.jack_base.datasource.common.aspect.annotation.DataSourceNameCut;
import com.jackstraw.jack_base.datasource.common.entity.enums.DataSourceTypeEnum;
import com.jackstraw.jack_base.datasource.mybatis.base.service.impl.MyBatisServiceImpl;
import com.jackstraw.jack_base.rules.dao.database.SceneActuatorDao;
import com.jackstraw.jack_base.rules.entity.bo.RuleGroupBo;
import com.jackstraw.jack_base.rules.entity.bo.SceneActuatorBo;
import com.jackstraw.jack_base.rules.entity.enums.ResultEnum;
import com.jackstraw.jack_base.rules.entity.mapstruct.SceneActuatorMapstruct;
import com.jackstraw.jack_base.rules.entity.po.SceneActuatorKeyPo;
import com.jackstraw.jack_base.rules.entity.po.SceneActuatorPo;
import com.jackstraw.jack_base.rules.entity.po.SceneActuatorPoExample;
import com.jackstraw.jack_base.rules.exception.BadRuleException;
import com.jackstraw.jack_base.rules.service.RuleGroupService;
import com.jackstraw.jack_base.rules.service.SceneActuatorService;
import com.jackstraw.jack_base.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 场景信息执行器表  service实现类
 *
 * @author zhaozhao 945514650@qq.com
 * @since version:1.0 generatorDate:2022-12-04 14:28:29
 */
@Slf4j
@DataSourceNameCut(dataSourceName = "dataBaseName1", dataSourceTypeEnum = DataSourceTypeEnum.database)
public class SceneActuatorServiceImpl
        extends MyBatisServiceImpl<SceneActuatorDao, SceneActuatorPo, SceneActuatorBo, SceneActuatorKeyPo<SceneActuatorPo>, SceneActuatorPoExample>
        implements SceneActuatorService {

    @Autowired
    private SceneActuatorMapstruct sceneActuatorMapstruct;

    @Autowired
    private RuleGroupService ruleGroupService;


    @Override
    protected SceneActuatorPoExample pe(Map<String, Object> params) {
        SceneActuatorPoExample pe = super.pe(params);
        //默认查询主键
        pe.addKey(params);
        return pe;
    }

    @Override
    public SceneActuatorBo queryOne(String sceneName) {
        SceneActuatorPo sceneActuatorPo = this.getDao().selectBySceneNameWithNoBlob(sceneName);
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(sceneActuatorPo)) {
            throw new BadRuleException(ResultEnum.SCENE_DATA_NO_FIND_BY_DATABASE);
        }

        if (!Objects.equals(1, sceneActuatorPo.getIsValid())) {
            throw new BadRuleException(ResultEnum.SCENE_DATA_LOSE_EFFICACY);
        }
        SceneActuatorBo sceneActuatorBo = sceneActuatorMapstruct.bo(sceneActuatorPo);
        return sceneActuatorBo;
    }

    @Override
    public SceneActuatorBo queryOneWithBlob(String sceneName) {
        SceneActuatorPo sceneActuatorPo = this.getDao().selectBySceneName(sceneName);
        if (ObjectUtil.ObjectUtilByLang3.isEmpty(sceneActuatorPo)) {
            throw new BadRuleException(ResultEnum.SCENE_DATA_NO_FIND_BY_DATABASE);
        }

        if (Objects.equals(0, sceneActuatorPo.getIsValid())) {
            throw new BadRuleException(ResultEnum.SCENE_DATA_LOSE_EFFICACY);
        }
        SceneActuatorBo sceneActuatorBo = sceneActuatorMapstruct.bo(sceneActuatorPo);
        return sceneActuatorBo;
    }

    @Override
    public SceneActuatorBo queryOneAll(String sceneName) {
        SceneActuatorBo sceneActuatorBo = this.queryOneWithBlob(sceneName);
        List<RuleGroupBo> ruleGroupBos = ruleGroupService.queryAllRelationGroupsById(sceneActuatorBo.getSceneActuatorId());
        sceneActuatorBo.setRuleGroupBos(ruleGroupBos);
        return sceneActuatorBo;
    }

    @Override
    public Integer querySceneVersion(String sceneName) {
        try {
            SceneActuatorBo sceneActuatorBo = this.queryOne(sceneName);
            return sceneActuatorBo.getVersion();
        } catch (BadRuleException badRuleException) {
            if (BadRuleException.errorCode(ResultEnum.SCENE_DATA_NO_FIND_BY_DATABASE.getCode()).equals(badRuleException.getErrorCode())) {
                return -1;
            }
            if (BadRuleException.errorCode(ResultEnum.SCENE_DATA_LOSE_EFFICACY.getCode()).equals(badRuleException.getErrorCode())) {
                return -2;
            }
        }
        return 0;
    }

    @Override
    public List<SceneActuatorBo> queryAllRules() {
        SceneActuatorPoExample sceneActuatorPoExample = new SceneActuatorPoExample();
        sceneActuatorPoExample.createCriteria()
                .andIsValidEqualTo(1);
        List<SceneActuatorPo> sceneActuatorPos = this.getDao().selectByExampleWithNoBlob(sceneActuatorPoExample);
        List<SceneActuatorBo> sceneActuatorBos = sceneActuatorMapstruct.boToS(sceneActuatorPos);
        return sceneActuatorBos;
    }
}
